package app.base.systemmanage.systempermsmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.base.systemmanage.systempermsmanage.dao.SysPermissionsDAO;
import app.base.systemmanage.systempermsmanage.model.SysPermissions;

public class SystemPermsFlatQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		int start = Integer.parseInt(URLDecoder.decode(request.getParameter("start"), Utility.CHARSET_ENCODING_UTF8));
	    int limit = Integer.parseInt(URLDecoder.decode(request.getParameter("limit"), Utility.CHARSET_ENCODING_UTF8));
	    
	    String cmd = URLDecoder.decode(request.getParameter("queryCmd"), Utility.CHARSET_ENCODING_UTF8);
	    
		String result = null;

		if (cmd == null || cmd.trim().length() == 0)
		{
			result = "{returnCode: '-1', msg: '客户端的数据请求命令参数CMD为设置错误'}";
		}
		else
		{
			cmd = URLDecoder.decode(cmd, Utility.CHARSET_ENCODING_UTF8).trim();
			
			String node = "%";
			String permName = "%";
			
			if (cmd.equals(Utility.SYS_OPERATION_COMMAND_QUERY))
			{
				permName = request.getParameter("permName");
				permName = (permName == null || permName.trim().length() == 0) ? "%" : URLDecoder.decode(permName, Utility.CHARSET_ENCODING_UTF8);
			}
			else
			{
				node = request.getParameter("node");
				node = (node == null || node.trim().length() == 0) ? "%" : URLDecoder.decode(node, Utility.CHARSET_ENCODING_UTF8);
				
				if (node.equals(Utility.SYSTEM_PERMS_MANAGE_ROOTNODE_ID))
				{
					cmd = Utility.SYS_OPERATION_COMMAND_QUERY;
				}
			}
			
		    Map<String, Object>  map = new HashMap<String, Object>();
		    
			map.put("start", BigDecimal.valueOf(start + 1));
			map.put("end", BigDecimal.valueOf(start + limit));
			map.put("cmd", cmd);
			map.put("permName", permName);
			map.put("node", node);
			
			result = getGridListData(map).toString();
		}
		
	    out.println(result);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	public StringBuffer getGridListData(Map<String, Object>  map) 
		throws UnsupportedEncodingException
	{
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysPermissionsDAO sysPermissionsDAO = (SysPermissionsDAO)context.getBean(Utility.SYSTEM_PERMISSIONS_MANAGE_MODULE_BEAN_NAME);
				
		List<SysPermissions> list = sysPermissionsDAO.select_0002(map);	
		
		StringBuffer json = new StringBuffer("{totalProperty:" + sysPermissionsDAO.count_0001(map) + ",root:[");
		
		for(int index = 0; index < list.size(); index++)
		{
			SysPermissions sysPermissions = (SysPermissions) list.get(index);
			
            json.append("{permId:'" + sysPermissions.getPERM_XTYPE_ID().trim()
             	+ "',permParentId:'" + sysPermissions.getPERM_XTYPE_PARENT_ID().trim() 
             	+ "',permParentName:'" + sysPermissions.getPERM_XTYPE_PARENT_NAME().trim() 
             	+ "',permName:'" + sysPermissions.getPERM_XTYPE_NAME().trim() 
            	+ "',permDesc:'" + ((sysPermissions.getPERM_XTYPE_DESC() == null) ? "" : sysPermissions.getPERM_XTYPE_DESC().trim())
            	+ "',permCode:'" + sysPermissions.getPERM_CODE().trim()
            	+ "',sysHoldFlag:'" + sysPermissions.getSYS_HOLD_FLAG().trim()
            	+ "',state:'" + sysPermissions.getSTATE().trim() + "'}");
            
            if (index < list.size() - 1) {
            	json.append(",");
            }
		}
		
		json.append("]}");
		
		return json;
	}
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		
	}

}